import classnames from 'classnames'

import Icon from '@/components/Icon'
import styles from './index.module.scss'
import { useDispatch, useSelector } from 'react-redux'
import { RootState } from '@/types/store'
import { differenceBy } from 'lodash'
import { useState } from 'react'
import {
  addUserChannel,
  changeActive,
  delUserChannel,
} from '@/store/action/home'
type Props = {
  hide: () => void
}
const Channels = ({ hide }: Props) => {
  const dispatch = useDispatch()
  const { active, userChannel, allChannel } = useSelector(
    (state: RootState) => state.home
  )
  // 得到一个推荐频道
  const recommendChannels = differenceBy(allChannel, userChannel, 'id')

  const onChange = (id: number) => {
    // 编辑状态不允许切换
    if (isEdit) return
    dispatch(changeActive(id))
    hide()
  }

  // 是否是编辑状态
  const [isEdit, setIsEdit] = useState(false)
  const changeEdit = () => {
    setIsEdit(!isEdit)
  }
  return (
    <div className={styles.root}>
      <div className="channel-header">
        <Icon type="iconbtn_channel_close" onClick={hide} />
      </div>
      <div className="channel-content">
        {/* 编辑时，添加类名 edit */}
        <div className={classnames('channel-item', isEdit && 'edit')}>
          <div className="channel-item-header">
            <span className="channel-item-title">我的频道</span>
            <span className="channel-item-title-extra">
              点击{isEdit ? '删除' : '进入'}频道
            </span>
            <span className="channel-item-edit" onClick={changeEdit}>
              {isEdit ? '完成' : '编辑'}
            </span>
          </div>
          <div className="channel-list">
            {/* 选中时，添加类名 selected */}
            {userChannel.map((item) => (
              <span
                key={item.id}
                className={classnames(
                  'channel-list-item',
                  item.id === active && 'selected'
                )}
                onClick={() => onChange(item.id)}
              >
                {item.name}
                {item.id !== 0 && (
                  <Icon
                    type="iconbtn_tag_close"
                    onClick={() => {
                      if (userChannel.length <= 4) return
                      if (item.id === active) {
                        dispatch(changeActive(0))
                      }
                      dispatch(delUserChannel(item.id))
                    }}
                  />
                )}
              </span>
            ))}
          </div>
        </div>

        <div className="channel-item">
          <div className="channel-item-header">
            <span className="channel-item-title">频道推荐</span>
            <span className="channel-item-title-extra">点击添加频道</span>
          </div>
          <div className="channel-list">
            {recommendChannels.map((item) => (
              <span
                key={item.id}
                className="channel-list-item"
                onClick={() => dispatch(addUserChannel(item))}
              >
                + {item.name}
              </span>
            ))}
          </div>
        </div>
      </div>
    </div>
  )
}

export default Channels
